package com.panda.mall.wares.brand.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.mall.common.constants.RedisConstants;
import com.panda.mall.common.mallWares.entity.MallGoodsBrand;
import com.panda.mall.common.utils.PinYinUtil;
import com.panda.mall.common.utils.RedisUtil;
import com.panda.mall.wares.brand.service.MallGoodsBrandService;
import com.panda.mall.wares.brand.mapper.MallGoodsBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author tucy123
* @description 针对表【mall_goods_brand(品牌表)】的数据库操作Service实现
* @createDate 2023-02-22 09:10:01
*/
@Service
@CacheConfig(cacheNames = "MallBrand")
public class MallGoodsBrandServiceImpl extends ServiceImpl<MallGoodsBrandMapper, MallGoodsBrand> implements MallGoodsBrandService{

    @Autowired
    MallGoodsBrandMapper mallGoodsBrandMapper;

    @Resource
    RedisUtil redisUtil;

    @Override
    @CacheEvict(value = "MallBrand", allEntries=true)
    public String saveOrUpdateBrand(MallGoodsBrand mallGoodsBrand) {

        if (ObjectUtil.isEmpty(mallGoodsBrand.getId())){
            Integer count = mallGoodsBrandMapper.queryCount();
            String initial = PinYinUtil.toPinYin(mallGoodsBrand.getBrandName());
            mallGoodsBrand.setSort(count);                                                   //排序号
            mallGoodsBrand.setFirstLetter(initial);                                          //首字母
        }

        boolean result = this.saveOrUpdate(mallGoodsBrand);

        return result ? "成功" : "失败";
    }

    @Override
    @Cacheable(key = "#root.methodName",unless = "#result = null")
    public List<MallGoodsBrand> queryBrand() {

        List<MallGoodsBrand> goodsBrands = this.baseMapper.selectList(null);

        return ObjectUtil.isNotEmpty(goodsBrands) ? goodsBrands : null;
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @Override
    @Cacheable(key = "#root.methodName + ':' +#id", unless = "#result = null")
    public List<MallGoodsBrand> queryBrandById(String id) {

        //缓存保存返回类型LinkedHashMap，因此查询用List，确保缓存可以有效
        QueryWrapper<MallGoodsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<MallGoodsBrand> brands = this.baseMapper.selectList(queryWrapper);

        return ObjectUtil.isNotEmpty(brands) ? brands : null;
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "MallBrand", allEntries = true)
    public Boolean deleteBrandById(String id) {

        int delete = this.baseMapper.deleteById(id);

        return (delete != 0);
    }
}
